/*
 * Copyright (c) 2010-2020 Belledonne Communications SARL.
 *
 * This file is part of linphone-android
 * (see https://www.linphone.org).
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package com.newlink.building.activities.main.fragments

import android.os.Bundle
import android.view.View
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.ViewModelProvider
import com.newlink.building.R
import com.newlink.building.activities.main.adapter.Module_Phone_SelectionListAdapter
import com.newlink.building.activities.main.viewmodels.Module_Phone_ListTopBarViewModel
import com.newlink.building.utils.Module_Phone_ToastUtils
import com.newlink.building.widget.Module_Phone_CustomAlertDialog
import org.linphone.core.tools.Log

/**
 * This fragment can be inherited by all fragments that will display a list
 * where items can be selected for removal through the ListTopBarFragment
 */
abstract class Module_Phone_MasterFragment<T : ViewDataBinding, U : Module_Phone_SelectionListAdapter<*, *>> :
    Module_Phone_SecureFragment<T>() {
    protected var _adapter: U? = null
    protected val adapter: U
        get() {
            if (_adapter == null) {
                Log.e("[Master Fragment] Attempting to get a null adapter!")
            }
            return _adapter!!
        }

    protected lateinit var listSelectionViewModel: Module_Phone_ListTopBarViewModel
    protected open val dialogConfirmationMessageBeforeRemoval: Int = R.plurals.dialog_default_delete

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        // List selection
        listSelectionViewModel = requireActivity().run {
            ViewModelProvider(this)[Module_Phone_ListTopBarViewModel::class.java]
        }

        listSelectionViewModel.isEditionEnabled.observe(
            viewLifecycleOwner
        ) {
            if (!it) listSelectionViewModel.onUnSelectAll()
        }

        listSelectionViewModel.selectAllEvent.observe(
            viewLifecycleOwner
        ) {
            it.consume {
                listSelectionViewModel.onSelectAll(getItemCount() - 1)
            }
        }

        listSelectionViewModel.unSelectAllEvent.observe(
            viewLifecycleOwner
        ) {
            it.consume {
                listSelectionViewModel.onUnSelectAll()
            }
        }

        listSelectionViewModel.deleteSelectionEvent.observe(
            viewLifecycleOwner
        ) {
            it.consume {
                if (listSelectionViewModel.selectedItems.value?.isEmpty() == true) {
                    Module_Phone_ToastUtils.showTextToast(
                        requireContext(),
                        getString(R.string.dialog_delete_content_no_select)
                    )
                } else {
                    requireContext().run {
                        val dialog = Module_Phone_CustomAlertDialog(this)
                        val title = getString(R.string.dialog_delete_content)
                        dialog.setContentText(title).setNegativeButton {
                            dialog.dismiss()
                            listSelectionViewModel.isEditionEnabled.value = false
                        }.setPositiveButton {
                            delete()
                            dialog.dismiss()
                            listSelectionViewModel.isEditionEnabled.value = false
                        }

                        dialog.show()
                    }
                }
            }
        }
    }

    private fun delete() {
        val list = listSelectionViewModel.selectedItems.value ?: arrayListOf()
        deleteItems(list)
    }

    private fun getItemCount(): Int {
        return adapter.itemCount
    }

    abstract fun deleteItems(indexesOfItemToDelete: ArrayList<Int>)
}
